/* 
 * Copyright (c) 2012, Fromentin Xavier, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *      * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *      * The names of its contributors may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Fromentin Xavier, Schnell Michaël, Dervin Cyrielle OR Brabant Quentin 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package kameleon.gui.view;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import kameleon.exception.FileReadingException;
import kameleon.exception.KameleonException;
import kameleon.gui.exception.UnknownKeyException;
import kameleon.gui.language.SwitchLanguage;
import kameleon.gui.model.FileInfo;
import kameleon.gui.model.GenerationModel;
import kameleon.gui.model.Model;
import kameleon.gui.util.FileConstants;
import kameleon.gui.util.ImageUtility;
import kameleon.gui.util.LanguageConstants;
import kameleon.plugin.PlugInInfo;

/**
 * Frame displaying the name and format of the files produced by the 
 * last generation of a given file.
 * 
 * @author		Dervin Cyrielle, Schnell Michaël
 * @version		1.0
 */
public class MoreInformationFrame extends JDialog 
implements LanguageConstants, FileConstants {

	/**
	 * Needed to serialize this class.
	 * 
	 * @see		java.io.Serializable
	 */
	private static final long serialVersionUID = -5336372109863612812L ; 
	
	/**
	 * Font used by bold text.
	 */
	protected final Font BOLD = new Font(
			this.getFont().getName(), Font.BOLD, this.getFont().getSize()) ;
	
	/**
	 * Model of this view.
	 */
	protected Model model ;
	
	/**
	 * Parent component for this dialog.
	 */
	protected JFrame container ;

	/**
	 * Builds a dialog displaying the name and format of the files produced by the 
	 * last generation of currently selected file.
	 * 
	 * @param	parent
	 * 			parent frame of this instance
	 * 
	 * @param	model
	 * 			model of this view
	 * 
	 * @throws 	KameleonException
	 * 			if an error occurred while build this instance
	 */
	public MoreInformationFrame(JFrame parent, Model model) throws KameleonException {
		super(parent, true) ;
		this.model = model ;
		this.initializeComponent() ;
		this.initializeWindow() ;
	}// MoreInformationFrame(JFrame, Model)

	/**
	 * Sets the properties (size, laf, icon, ...) of the dialog.
	 * 
	 * @throws	UnknownKeyException
	 * 			if a key for a displayed text could not be found
	 */
	protected void initializeWindow() throws UnknownKeyException {
		this.setTitle(SwitchLanguage.getInstance().getText(ABOUT, 
				this.model.getSelectedFileInfo().getName())) ;
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE) ;
		this.pack() ;
		this.setLocationRelativeTo(this.container) ;
		this.setResizable(false) ;
	}// initializeWindow()

	/**
	 * Builds the content of this dialog.
	 * 
	 * @throws 	KameleonException 
	 * 			If an error occurred while building the dialog
	 */
	protected void initializeComponent() throws KameleonException {
		GridBagLayout gridbag = new GridBagLayout() ;
		GridBagConstraints c ;
		this.setLayout(gridbag) ;

		// File entries
		c = new GridBagConstraints() ;
		c.gridwidth = GridBagConstraints.REMAINDER ;
		c.fill = GridBagConstraints.VERTICAL ;
		LastGeneratedView fileEntries = new LastGeneratedView() ;
		this.add(fileEntries) ;
		gridbag.setConstraints(fileEntries, c) ;
	}// initializeComponent()

	/**
	 * View displaying the name and format of each generated file.
	 * 
	 * @author	Dervin Cyrielle, Schnell Michaël
	 * @version	1.0
	 */
	class LastGeneratedView extends JPanel {
		
		/**
		 * Needed to serialize this class.
		 * 
		 * @see		java.io.Serializable
		 */
		private static final long serialVersionUID = -6744854757301952269L ;

		/**
		 * Sole constructor.
		 * 
		 * @throws 	KameleonException
		 * 			If an error occurred while building the view 
		 */
		public LastGeneratedView() throws KameleonException {
			super() ;
			this.initialize() ;
		}// LastGeneratedView()

		/**
		 * Builds the content of this view.
		 * 
		 * @throws 	UnknownKeyException 
		 * 			if a key for a displayed text could not be found
		 * 
		 * @throws 	FileReadingException
		 * 			If a format icon could not be loaded
		 */
		protected void initialize() throws UnknownKeyException, FileReadingException {
			GridBagLayout gridbag = new GridBagLayout() ;
			this.setLayout(gridbag) ;

			this.buildTitle(gridbag) ;
			this.buildFileEntries(gridbag) ;
		}// initialize()
		
		/**
		 * Builds the introduction sentence displayed in this dialog.
		 * 
		 * @param	gridbag 
		 * 			layout manager used by this frame
		 * 
		 * @throws	UnknownKeyException 
		 * 			if the key for the explanation text was not found
		 */
		private void buildTitle(GridBagLayout gridbag) 
				throws UnknownKeyException {
			SwitchLanguage sl = SwitchLanguage.getInstance() ;
			GridBagConstraints constraints ;
			
			constraints = new GridBagConstraints() ;
			constraints.gridwidth = GridBagConstraints.REMAINDER ;
			constraints.anchor = GridBagConstraints.BASELINE ;
			constraints.weightx = 1.0 ;
			constraints.weighty = 0.0 ;
			constraints.insets = new Insets(5, 5, 5, 5) ;
			
			JLabel label = new JLabel(sl.getText(LAST_GENERATED)) ;
			
			this.add(label) ;
			gridbag.setConstraints(label, constraints) ;
		}// buildTitle(GridBagLayout)
		
		/**
		 * Builds the entries for the analyzers, one per analyzer.
		 * Entries are displayed using the {@link ViewPlugInInfo} class.
		 * 
		 * @param	gridbag 
		 * 			layout manager used by this frame
		 * 
		 * @throws 	UnknownKeyException 
		 * 			if a key for a displayed text could not be found
		 * 
		 * @throws 	FileReadingException
		 * 			If a format icon could not be loaded
		 */
		private void buildFileEntries(GridBagLayout gridbag) 
				throws UnknownKeyException, FileReadingException {
			SwitchLanguage sl = SwitchLanguage.getInstance() ;
			GenerationModel gModel = MoreInformationFrame.this.model ;
			GridBagConstraints iconConstraints, titleConstraints,
				valueConstraint ;
			
			iconConstraints = new GridBagConstraints() ;
			iconConstraints.gridwidth  = 1 ;
			iconConstraints.gridheight = 3 ;
			iconConstraints.anchor = GridBagConstraints.CENTER ;
			iconConstraints.weightx = 0.0 ;
			iconConstraints.weighty = 1.0 ;
			iconConstraints.insets = new Insets(5, 5, 5, 5) ;
			
			titleConstraints = new GridBagConstraints() ;
			titleConstraints.gridwidth = 1 ;
			titleConstraints.anchor = GridBagConstraints.BASELINE_LEADING ;
			titleConstraints.weightx = 0.0 ;
			titleConstraints.weighty = 0.0 ;
			titleConstraints.insets = new Insets(5, 0, 0, 5) ;
			
			valueConstraint = new GridBagConstraints() ;
			valueConstraint.gridwidth = GridBagConstraints.REMAINDER ;
			valueConstraint.anchor = GridBagConstraints.BASELINE_LEADING ;
			valueConstraint.weightx = 1.0 ;
			valueConstraint.weighty = 0.0 ;
			
			FileInfo lastG = gModel.getSelectedFileInfo() ;
			String[] formatIds = lastG.getTargetFormatId() ;
			String[] targetPaths = lastG.getTargetPath() ;
			int nGeneratedFiles = lastG.getTargetPath().length ;
			for(int file=0; file<nGeneratedFiles; file++) {
				PlugInInfo genInfo = gModel.getGenerator(formatIds[file]) ;
				String iconName = String.format(COLOR_ICON, genInfo.getId()) ;
				String formatName = genInfo.getFormatName() ;

				JLabel formatTitle = new JLabel(sl.getText(FORMAT)) ;
				JLabel pathTitle = new JLabel(sl.getText(PATH)) ;
				JLabel icon = new JLabel(
						new ImageIcon(ImageUtility.getImageBytes(iconName))) ;
				icon.setToolTipText(formatName) ;
				JLabel formatNameLabel = new JLabel(formatName) ;	
				JLabel path = new JLabel(targetPaths[file]) ;
				formatNameLabel.setFont(MoreInformationFrame.this.BOLD) ;
				path.setFont(MoreInformationFrame.this.BOLD) ;
				
				this.add(icon) ;
				gridbag.setConstraints(icon, iconConstraints) ;
				this.add(formatTitle) ;
				gridbag.setConstraints(formatTitle, titleConstraints) ;
				this.add(formatNameLabel) ;
				gridbag.setConstraints(formatNameLabel, valueConstraint) ;
				this.add(pathTitle) ;
				gridbag.setConstraints(pathTitle, valueConstraint) ;
				this.add(path) ;
				gridbag.setConstraints(path, valueConstraint) ;
			}// for
		}// buildFileEntries(GridBagLayout)
	
	}// class LastGeneratedView

}// class MoreInformationFrame